I want a single regular expression that will be true for strings starting with "BPv2" but not for strings starting with "BPv2.5" if (matches("^BPv2", myString) and !matches("^BPv2.5", myString)) if there is no single regexp that satisfies, I may have to re-write the code that this is a part of, to use the multi-regexp method. Any help is appreciated!
Thosicus - Tue Jun 11 17:38:39 EDT 2013 |
Re: Regular Expression help Hi Thosicus I think some clarification of your requirement is needed. Do you need to identify text that contains only 'BPv2', or do you also need 'BPv2.1'-'BPv2.4', but excluding 2.5? What about values higher than 2.5? If what you want is only values of 'BPv2', with absolutely nothing else before or after in the string, how about: ^BPv2$ Your expression fails because the part in square brackets is required - in other words, you're requiring at least one character after 'BPv2', but asserting only that it must not be a '.' You may be aware that you can add a '*' after the square brackets to denote that the character(s) may occur zero or more times, or a '+' to denote that it occurs at least once, but neither will help in this specific case. I hope this helps - if you're able to clarify your requirement, perhaps more can be done. Regards, Richard |
Re: Regular Expression help Richard_Hesketh - Wed Jun 12 02:54:32 EDT 2013 Hi Thosicus I think some clarification of your requirement is needed. Do you need to identify text that contains only 'BPv2', or do you also need 'BPv2.1'-'BPv2.4', but excluding 2.5? What about values higher than 2.5? If what you want is only values of 'BPv2', with absolutely nothing else before or after in the string, how about: ^BPv2$ Your expression fails because the part in square brackets is required - in other words, you're requiring at least one character after 'BPv2', but asserting only that it must not be a '.' You may be aware that you can add a '*' after the square brackets to denote that the character(s) may occur zero or more times, or a '+' to denote that it occurs at least once, but neither will help in this specific case. I hope this helps - if you're able to clarify your requirement, perhaps more can be done. Regards, Richard Hi Richard. I had tried the * and the + characters to no avail in this case, as you said. Here are some example strings with desired rx results:
BPv2 -> true The original attribute values are linebreak-separated, as in ...
"BPv2.5 ...but I added a little function in the code to remove the line breaks, as I thought it would make the rx checking work. It didn't. Thanks, T |
Re: Regular Expression help Maybe there is a couple more test cases, but this seems to work:
I think it says: Any string that matches any of these conditions:
Here's the code I used to test it.
string sBP
void Test(string s)
// Set("^(BPv2)$") // true false++
print "\t\tsBP = {" sBP "}\n" -Louie |
Re: Regular Expression help llandale - Wed Jun 12 13:07:09 EDT 2013 Maybe there is a couple more test cases, but this seems to work:
I think it says: Any string that matches any of these conditions:
Here's the code I used to test it.
string sBP
void Test(string s)
// Set("^(BPv2)$") // true false++
print "\t\tsBP = {" sBP "}\n" -Louie Hi Louie, Amazing! That worked -- for my initial set of test cases. When I tried a BPv2.7 (which I was told was a possible value), it gave a true; so I tried removing the last rx chunk that deals with BPv2.5, and it then gave a correct false for BPv2.5 as well as for BPv2.7. It also gave correct true/false results for my other test strings. The resulting regexp in my test code now looks like this:
Regexp BPOK = regexp ("^(BPv2\\.*|BPv2[^\\.].*)$") // Louie Landale's suggestion, edited Kudos and high-fives to you Mr. Landale. -T |
Re: Regular Expression help Thosicus - Wed Jun 12 13:56:33 EDT 2013 Hi Louie, Amazing! That worked -- for my initial set of test cases. When I tried a BPv2.7 (which I was told was a possible value), it gave a true; so I tried removing the last rx chunk that deals with BPv2.5, and it then gave a correct false for BPv2.5 as well as for BPv2.7. It also gave correct true/false results for my other test strings. The resulting regexp in my test code now looks like this:
Regexp BPOK = regexp ("^(BPv2\\.*|BPv2[^\\.].*)$") // Louie Landale's suggestion, edited Kudos and high-fives to you Mr. Landale. -T "Mr. Landale" died 15 years ago. Thanks for reminding me. lol Your expression fails for "BPv2.BPv3" when it should pass. I think your new requirements are for it to pass for major version "2" but to fail for any well-formed minor version, 2.1, 2.2, ... 2.9. I'm guessing that "2.0" should pass. If so, replace the "5" in my string with "1-9"; which then looks like:
ButWAIT! Should the following string pass or fail?
But WAIT! The "^" and "$" characters, when at the start and end of the expression, means start or end of the line, even though the manual says start/end of the string. Since we know that a two "lines" have the '\n' end-of-line delimiter, I see that my expression PASSES the following input string when it should FAIL:
Wikipedia Regular Expressions talks about "greedy" and "non-greedy" expressions and I'm pretty sure the solution can be found by playing with that. Regular Expressions always give me Regular Headaches. -Louie |
Re: Regular Expression help llandale - Wed Jun 12 14:26:48 EDT 2013 "Mr. Landale" died 15 years ago. Thanks for reminding me. lol Your expression fails for "BPv2.BPv3" when it should pass. I think your new requirements are for it to pass for major version "2" but to fail for any well-formed minor version, 2.1, 2.2, ... 2.9. I'm guessing that "2.0" should pass. If so, replace the "5" in my string with "1-9"; which then looks like:
ButWAIT! Should the following string pass or fail?
But WAIT! The "^" and "$" characters, when at the start and end of the expression, means start or end of the line, even though the manual says start/end of the string. Since we know that a two "lines" have the '\n' end-of-line delimiter, I see that my expression PASSES the following input string when it should FAIL:
Wikipedia Regular Expressions talks about "greedy" and "non-greedy" expressions and I'm pretty sure the solution can be found by playing with that. Regular Expressions always give me Regular Headaches. -Louie Master Landale, The attribute this is looking at is a multi-enum type consisting of these strings:
787 Anything starting with BPv2 (but not BPv2.5) is to be counted. So I think we're covered now, THANKS to you. You think you've got it bad? Regular expressions make me irregular. -T |
Re: Regular Expression help Thosicus - Wed Jun 12 16:08:38 EDT 2013 Master Landale, The attribute this is looking at is a multi-enum type consisting of these strings:
787 Anything starting with BPv2 (but not BPv2.5) is to be counted. So I think we're covered now, THANKS to you. You think you've got it bad? Regular expressions make me irregular. -T
If you need to future proof this against adding a "BPv20, BPv21, BPv23..." enumeration I would use this:
-Adam
|
Re: Regular Expression help Thosicus - Wed Jun 12 16:08:38 EDT 2013 Master Landale, The attribute this is looking at is a multi-enum type consisting of these strings:
787 Anything starting with BPv2 (but not BPv2.5) is to be counted. So I think we're covered now, THANKS to you. You think you've got it bad? Regular expressions make me irregular. -T Then your requirements are "if it is exactly 'BPv2'"; forget about "starts with". |